package edu.mtu.citizenscience.gpsservice;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

/*
 * This is service for providing access to the CSLocationListener.
 * The method onCreate() is only called for the first binding and creates the CSLocationListener.
 */

public class CSGpsService extends Service {

	private CSLocationListener locationListener;
	private final IBinder mCSGpsBinder = new CSGpsBinder();
	
	public static final String LOCATION_SERVICE = "LOCATION_SERVICE"; // key for intent
	public static final String VALID_LOCATION = "VALID_LOCATION"; // key for check valid Location
	
	/*
	 * Keys and values for setting location listener
	 */
	public static final String UPDATE_RATE = "UPDATE_RATE"; // this is the key
	public static final String TIGHT = "TIGHT";  // this and below are the values
	public static final String LOOSE = "LOOSE";
	
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId)
	{
		Log.d("CSGpsService", "onStartCommand");
		/*
		 * Examine the intent for the service settings
		 */
		String updateRate = intent.getStringExtra(this.UPDATE_RATE);
		if(updateRate.equalsIgnoreCase(TIGHT)) this.locationListener.setTightUpdates();
		else if(updateRate.equalsIgnoreCase(LOOSE)) this.locationListener.setLooseUpdates();
		
		return START_STICKY;
	}
	
	@Override
	public IBinder onBind(Intent intent) { return mCSGpsBinder; }
	
	public class CSGpsBinder extends Binder
	{
		public CSLocationListener getLocationListener() 
		{
			return locationListener;
		}
	}
	
	/**
	 * onCreate() is called before onStart
	 */
	@Override 
	public void onCreate() 
	{
		Log.d("CSGPSService","onCreate");
		
		/*
		 * locationListener must be created in onCreate not above in the implicit constructor
		 */
		locationListener = new CSLocationListener(this);
		this.locationListener.start();
	}
	
	@Override
	public void onDestroy()
	{
		this.locationListener.stop();
	}
	

			
}
